package cn.datawin.bms.modules.deploy.controller;

import cn.datawin.bms.deploy.entity.DeplServerModuleInfoEntity;
import cn.datawin.bms.modules.deploy.service.DeplServerModuleInfoService;
import cn.datawin.common.core.util.R;
import cn.datawin.common.log.annotation.SysLog;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 服务器端口信息表
 *
 * @author datawin
 * @date 2025-03-13 13:55:38
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/deploy/serverModuleInfo" )
@Tag(description = "serverModuleInfo" , name = "服务器端口信息表管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class DeplServerModuleInfoController {

    private final DeplServerModuleInfoService deplServerModuleInfoService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param serverModuleInfo 服务器端口信息表
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('deploy_serverModuleInfo_view')" )
    public R getServerModuleInfoPage(@ParameterObject Page page, @ParameterObject DeplServerModuleInfoEntity serverModuleInfo) {
        return R.ok(deplServerModuleInfoService.customPage(page, serverModuleInfo));
    }


	/**
	 * 查询模块下的所有未被占用的端口
	 * @param moduleId 模块id
	 */
	@Operation(summary = "查询所有模块" , description = "查询所有模块" )
	@GetMapping("/listByModule" )
	@PreAuthorize("isAuthenticated()" )
	public R getModuleInfoList(Integer moduleId) {
		LambdaQueryWrapper<DeplServerModuleInfoEntity> wrapper = Wrappers.lambdaQuery();
		wrapper.eq(DeplServerModuleInfoEntity::getModuleId,moduleId);
		wrapper.eq(DeplServerModuleInfoEntity::getStatus,0);
		return R.ok(deplServerModuleInfoService.list(wrapper));
	}

    /**
     * 通过id查询服务器端口信息表
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('deploy_serverModuleInfo_view')" )
    public R getById(@PathVariable("id" ) Integer id) {
        return R.ok(deplServerModuleInfoService.getById(id));
    }

    /**
     * 新增服务器端口信息表
     * @param serverModuleInfo 服务器端口信息表
     * @return R
     */
    @Operation(summary = "新增服务器端口信息表" , description = "新增服务器端口信息表" )
    @SysLog("新增服务器端口信息表" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('deploy_serverModuleInfo_add')" )
    public R save(@RequestBody DeplServerModuleInfoEntity serverModuleInfo) {
        return R.ok(deplServerModuleInfoService.save(serverModuleInfo));
    }

    /**
     * 修改服务器端口信息表
     * @param serverModuleInfo 服务器端口信息表
     * @return R
     */
    @Operation(summary = "修改服务器端口信息表" , description = "修改服务器端口信息表" )
    @SysLog("修改服务器端口信息表" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('deploy_serverModuleInfo_edit')" )
    public R updateById(@RequestBody DeplServerModuleInfoEntity serverModuleInfo) {
        return R.ok(deplServerModuleInfoService.updateById(serverModuleInfo));
    }

    /**
     * 通过id删除服务器端口信息表
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除服务器端口信息表" , description = "通过id删除服务器端口信息表" )
    @SysLog("通过id删除服务器端口信息表" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('deploy_serverModuleInfo_del')" )
    public R removeById(@RequestBody Integer[] ids) {
        return R.ok(deplServerModuleInfoService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param serverModuleInfo 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('deploy_serverModuleInfo_export')" )
    public List<DeplServerModuleInfoEntity> export(DeplServerModuleInfoEntity serverModuleInfo, Integer[] ids) {
        return deplServerModuleInfoService.list(Wrappers.lambdaQuery(serverModuleInfo).in(ArrayUtil.isNotEmpty(ids), DeplServerModuleInfoEntity::getId, ids));
    }
}
